package com.example.star.view.setting;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.star.R;
import com.example.star.api_service.ApiAddress;
import com.example.star.entity.Address;
import com.example.star.retrofit.ApiClient;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddressActivity extends AppCompatActivity {
    private EditText addressEdit;
    private Button addAddressButton;
    private TextView emptyView;
    private ListView listView;
    private ApiAddress apiAddress;
    private List<String> addressList = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private String userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        // 初始化 Retrofit
        apiAddress = ApiClient.getApiAddress();

        addressEdit = findViewById(R.id.address_edit);
        addAddressButton = findViewById(R.id.add_address_button);
        emptyView = findViewById(R.id.empty_view);
        listView = findViewById(R.id.listView);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, addressList);
        listView.setAdapter(adapter);

        // 从 SharedPreferences 中获取用户名
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        userName = sharedPreferences.getString("username", "");

        addAddressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String address = addressEdit.getText().toString();
                if (!address.isEmpty()) {
                    addAddress(address);
                } else {
                    Toast.makeText(AddressActivity.this, "请输入地址", Toast.LENGTH_SHORT).show();
                }
            }
        });

        getAddressByUserName(userName);
    }

    private void addAddress(String address) {
        Address newAddress = new Address();
        newAddress.setAddress(address);
        newAddress.setUserName(userName);
        Call<Address> call = apiAddress.saveAddress(newAddress);
        call.enqueue(new Callback<Address>() {
            @Override
            public void onResponse(Call<Address> call, Response<Address> response) {
                if (response.isSuccessful()) {
                    getAddressByUserName(userName);
                    addressEdit.setText("");
                    Toast.makeText(AddressActivity.this, "地址添加成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AddressActivity.this, "地址添加失败: " + response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Address> call, Throwable t) {
                Toast.makeText(AddressActivity.this, "网络请求失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getAddressByUserName(String userName) {
        Call<List<Address>> call = apiAddress.getAddressByUserName(userName);
        call.enqueue(new Callback<List<Address>>() {
            @Override
            public void onResponse(Call<List<Address>> call, Response<List<Address>> response) {
                if (response.isSuccessful()) {
                    addressList.clear();
                    List<Address> addresses = response.body();
                    if (addresses != null && !addresses.isEmpty()) {
                        emptyView.setVisibility(View.GONE);
                        for (Address address : addresses) {
                            addressList.add(address.getAddress());
                        }
                    } else {
                        emptyView.setVisibility(View.VISIBLE);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(AddressActivity.this, "获取地址失败: " + response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Address>> call, Throwable t) {
                Toast.makeText(AddressActivity.this, "网络请求失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}